package com.css.zfzx.sjcj.modules.service;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author Zyl
 * @Date 2021/4/14 9:35
 * @Version 1.0
 * @Describe: 获取接口的所有实现类名
 */
@Component
public class ServiceLocator implements ApplicationContextAware {

    //1.借助Spring容器实现
    /**
     * 用于保存接口实现类名及对应的类
     */
    private Map<String, CommonInterfaceService> map;

    /**
     * 获取应用上下文并获取相应的接口实现类
     * @param applicationContext
     * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        //根据接口类型返回相应的所有bean
        map = applicationContext.getBeansOfType(CommonInterfaceService.class);
    }

    public Map<String, CommonInterfaceService> getMap() {
        return map;
    }
}
